package com.turbo.cloud.core.exception.advice;

import com.turbo.cloud.core.api.R;
import com.turbo.cloud.core.api.Result;
import com.turbo.cloud.core.exception.BusinessException;
import com.turbo.cloud.core.exception.ForbiddenException;
import com.turbo.cloud.core.exception.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Result handleBusinessException(BusinessException e) {
        return R.failed(e.getMessage());
    }

    @ExceptionHandler(ForbiddenException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ResponseBody
    public Result handleForbiddenException(ForbiddenException e) {
        return R.failed(e.getMessage());
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Result handleUnauthorizedException(UnauthorizedException e) {
        return R.failed(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Result handleException(Exception e) {
        return R.failed(e.getMessage());
    }
}
